using Unity.VisualScripting;
using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    [UnitShortTitle("Data Node")]
    [UnitSurtitle("Get")]
    public sealed class GetDataNode : DataNodeUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Path { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput Value { get; private set; }

        protected override void Definition()
        {
            base.Definition();
            Path = ValueInput<string>(nameof(Path), string.Empty);
            Value = ValueOutput<object>(nameof(Path));
            Requirement(Path, Value);
        }

        protected override ControlOutput In(Flow flow)
        {
            string path = flow.GetValue<string>(Path);
            flow.SetValue(Value, Target?.GetData(path)?.GetValue() ?? default);
            return base.In(flow);
        }
    }
}